
package com.example.demo;
import com.example.demo.entity.User;
import com.example.demo.repository.UserRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.Scanner;

@Component
public class ConsoleApp implements CommandLineRunner {

    private final UserRepository userRepository;
    private final Scanner scanner = new Scanner(System.in);

    public ConsoleApp(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public void run(String... args) {
        while (true) {
            printMenu();
            int choice = scanner.nextInt();
            scanner.nextLine(); // 清除换行符

            switch (choice) {
                case 0 -> showAllUsers();
                case 1 -> addUser();
                case 2 -> deleteAllUsers();
                case 3 -> deleteUserById();
                case 4 -> findUserById();
                case 5 -> {
                    System.out.println("退出系统");
                    System.exit(0);
                }
                default -> System.out.println("无效输入，请重新选择");
            }
        }
    }

    private void printMenu() {
        System.out.println("""
            --------------------------
            用户管理系统
            0: 显示所有用户
            1: 添加用户
            2: 删除所有用户
            3: 按ID删除用户
            4: 按ID查询用户
            5: 退出
            --------------------------""");
        System.out.print("请选择操作(0-5): ");
    }

    private void showAllUsers() {
        System.out.println("\n当前用户列表:");
        userRepository.findAll().forEach(user ->
                System.out.printf("%d %s %s %s%n",
                        user.getId(),
                        user.getName(),
                        user.getEmail(),
                        "***" // 密码不直接显示
                )
        );
    }

    private void addUser() {
        System.out.print("请输入用户名: ");
        String name = scanner.nextLine();

        System.out.print("请输入邮箱: ");
        String email = scanner.nextLine();

        System.out.print("请输入密码: ");
        String password = scanner.nextLine();

        User user = new User();
        user.setName(name);
        user.setEmail(email);
        user.setPassword(password); // 假设User实体已添加password字段

        User savedUser = userRepository.save(user);
        System.out.printf("新增用户: %d %s %s %s%n",
                savedUser.getId(),
                savedUser.getName(),
                savedUser.getEmail(),
                savedUser.getPassword()
        );
    }

    private void deleteAllUsers() {
        userRepository.deleteAll();
        System.out.println("已删除所有用户");
    }

    private void deleteUserById() {
        System.out.print("请输入要删除的用户ID: ");
        Long id = scanner.nextLong();
        userRepository.deleteById(id);
        System.out.println("已删除用户: " + id);
    }

    private void findUserById() {
        System.out.print("请输入要查询的用户ID: ");
        Long id = scanner.nextLong();
        userRepository.findById(id).ifPresentOrElse(
                user -> System.out.printf("查询结果: %d %s %s%n",
                        user.getId(),
                        user.getName(),
                        user.getEmail()
                ),
                () -> System.out.println("用户不存在")
        );
    }
}